
import 'package:DiamondDiary/database/db_helper.dart';
import 'package:DiamondDiary/database/note_info.dart';
import 'package:DiamondDiary/pages/note_edit_page.dart';
import 'package:DiamondDiary/utils/route_helper.dart';
import 'package:DiamondDiary/utils/string_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

import 'note_preview_page.dart';

class HomePage extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    return HomePageState();
  }

}

class HomePageState extends State<HomePage>{

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(leading:null,title: Center(child:Text("首页"),),),
      body: HomeBody(),
      floatingActionButton:  FloatingActionButton(child:Icon(Icons.add_circle,size: 30,),
        onPressed: ()=>RouteHelper.routeTo(context, NoteEditPage())
            .then((value) => setState((){}))
      ),
    );
  }

}

class HomeBody extends StatefulWidget{

  @override
  State<StatefulWidget> createState() {
    return HomeBodyState();
  }

}

class HomeBodyState extends State<HomeBody>{

  var list = <Map<String,dynamic>>[];
  var refresher = EasyRefreshController();

  Future<void> loadData({int page}) async {
    var data = await DBHelper().getNoteInfo(limit: 10,offset:page??list.length);
    print(data.toString());
    if(data is List<Map>){
      if(page==0){
        list.clear();
      }
      setState((){list.addAll(data);});
    }
    refresher.finishLoad(success:true,noMore: data.isEmpty||data.length<10);
  }

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(milliseconds: 100),()=>loadData(page:0));
  }

  @override
  Widget build(BuildContext context) {
    //日历相关控件
    //列表
    //分页
    return EasyRefresh(
      controller: refresher,
      onRefresh: ()=>loadData(page:0),
      onLoad: ()=>loadData(),
      child: /*CustomScrollView(
        slivers: [*/
          ListView.separated(
            shrinkWrap: true,
            itemCount: list.length,
            separatorBuilder: (context,index){
              return Divider(height: 1,thickness: 1,);
            },
            itemBuilder: (context,index){
              var item = list[index];
              String content = item['content'];
              content = content.length>20?content.substring(0,20):content;
              content = content.replaceAll(RegExp('[\r\n\t]'), ' ');
              return InkWell(
                onTap: ()=>RouteHelper.routeTo(context, NotePreviewPage(info:NoteInfo.fromJson(item))),
                child:Padding(
                padding: EdgeInsets.fromLTRB(10, 8, 10, 8),
                child:Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Padding(padding: EdgeInsets.fromLTRB(10,3,10, 0),
                      child: Icon(Icons.circle,size: 10,color: Colors.green,),
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(StringUtils.formatTime(item['commitTime'])+ "   "+ item['title']),
                        Padding(child: Text(content),//需要限制长度
                          padding: EdgeInsets.only(top: 8),
                        )
                      ],
                    )
                  ],
                ),),
              );
            },
          ),
        );
  }


}